﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;

namespace QLTV.Forms.KeHoach
{
    public partial class frmTrainingCrew : BaseForm
    {
        public frmTrainingCrew()
        {
            InitializeComponent();
        }
        public IList datasourse { get; set; }
        public int thang { get; set; }
        public int nam { get; set; }
        public int courseID { get; set; }
        public bool isNew { get; set; }

        private void frmTrainingPlan_Load(object sender, EventArgs e)
        {
            loadData();
        }
        int planId;
        private void loadData()
        {
            if (tblTraningPlanTableAdapter.GetDataBy(courseID, thang, nam).Rows.Count > 0)
            {
                tblTraningPlanTableAdapter.FillBy(dsTraining.tblTraningPlan, courseID, thang, nam);
                using (Database.dsTrainingTableAdapters.tblTrainingPlanDetailTableAdapter tadDetail = new Database.dsTrainingTableAdapters.tblTrainingPlanDetailTableAdapter())
                {
                    tadDetail.FillBy(dsTraining.tblTrainingPlanDetail, (Int32)txtPlanID.EditValue);
                    layoutPlanDetail.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                    layoutOverview.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                }
            }
            else
            {
                planId = Ultilities.getNextID("tblTraningPlan");
                tblTraningPlanBindingSource.AddNew();
                txtMonHoc.EditValue = courseID;
                txtNgayLap.EditValue = DateTime.Now.Date;
                txtNguoiLap.EditValue = Program.EmployeeName;
                this.DataBindings.Add(new System.Windows.Forms.Binding("thang", this.tblTraningPlanBindingSource, "Thang", true));
                this.DataBindings.Add(new System.Windows.Forms.Binding("nam", this.tblTraningPlanBindingSource, "Nam", true));
                if (Program.language == "en")
                {
                    txtPlanName.Text = String.Format("Plan in {0}/{1}", thang, nam);
                }
                else
                {
                    txtPlanName.Text = String.Format("Kế hoạch tháng {0} Năm {1}", thang, nam);
                }
                gridOverview.DataSource = datasourse;
                layoutPlanDetail.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layoutOverview.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            }

            // TODO: This line of code loads data into the 'dsShortPlan.tblKeHoach' table. You can move, or remove it, as needed.
            this.tblKeHoachTableAdapter.Fill(this.dsShortPlan.tblKeHoach);
            // TODO: This line of code loads data into the 'dsShortPlan.tblShortPlan' table. You can move, or remove it, as needed.
            this.tblShortPlanTableAdapter.returnUndonePLan(this.dsShortPlan.tblShortPlan);
            // TODO: This line of code loads data into the 'dsTraining.tblCrewInfo' table. You can move, or remove it, as needed.
            this.tadCrewInfo.Fill(this.dsTraining.tblCrewInfo);
            this.tblTrainingCourseTableAdapter.Fill(this.dsTraining.tblTrainingCourse);
        }



        private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.RowHandle > -1)
                if (e.Column == colSTT)
                {
                    e.DisplayText = (e.RowHandle + 1).ToString();
                }
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            //if (viewTrainingPlan.FocusedRowHandle > -1)
            //{
            //    tblShortPlanBindingSource.Filter = String.Format("nCrewIDNext='{0}'", viewTrainingPlan.GetFocusedRowCellValue("nCrewId"));
            //    if (gridControl2.DataSource == null)
            //        gridControl2.DataSource = tblShortPlanBindingSource;
            //}
        }

        private void viewDetail_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.RowHandle > -1)
                if (e.Column == sttCol)
                {
                    e.DisplayText = (e.RowHandle + 1).ToString();
                }
        }

        private void repositoryItemButtonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (XtraMessageBox.Show(rm.GetString("sConfirm", myCultureInfo), rm.GetString("sMessageCaption", myCultureInfo), MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
            {
                viewTrainingPlan.DeleteSelectedRows();
            }
        }

        private void repositoryItemButtonEdit2_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (XtraMessageBox.Show(rm.GetString("sConfirm", myCultureInfo), rm.GetString("sMessageCaption", myCultureInfo), MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
            {
                viewDetail.SetFocusedRowCellValue("isDelete", true);
            }
        }
        public void setTotal(int thang, int nam, int courseid, int count)
        {
            string commText = String.Format("update tblTrainingOverView set thang{0}= '{1}'" + (isNew ? ", Scheduled=Scheduled+'" + thang + ",'" : "") + " where Nam='{2}' and nTrainingCourse='{3}'", thang, count, nam, courseid);
            string connStr = ConfigurationManager.ConnectionStrings["QLTV.Properties.Settings.CrewManagmentConnectionString"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                SqlCommand comm = new SqlCommand(commText, conn);
                comm.ExecuteNonQuery();
            }

        }



        private void viewDetail_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            //if (viewDetail.FocusedRowHandle > -1)
            //{
            //    tblShortPlanBindingSource.Filter = String.Format("nCrewIDNext='{0}'", viewDetail.GetFocusedRowCellValue("nCrewId"));
            //    if (gridControl2.DataSource == null)
            //        gridControl2.DataSource = tblShortPlanBindingSource;
            //}
        }


        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            save();
            this.Close();
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if ((Int32)txtPlanID.EditValue > 0)
            {
                Reports.rptTrainingCrewCourse r = new Reports.rptTrainingCrewCourse();
                r.CreatedDate = (DateTime)txtNgayLap.EditValue;
                r.CourseName = txtMonHoc.Text;
                r.loadData((Int32)txtPlanID.EditValue);
                r.ShowPreview();
            }
            else
            {
                if (XtraMessageBox.Show(rm.GetString("sInsertFirst", myCultureInfo),  rm.GetString("sMessageCaption", myCultureInfo), MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                {
                    save();

                }
            }
        }
        private void save()
        {
            if (layoutOverview.Visibility == DevExpress.XtraLayout.Utils.LayoutVisibility.Always)
            {
                tblTraningPlanBindingSource.EndEdit();
                tblTraningPlanTableAdapter.Update(dsTraining.tblTraningPlan);
                for (int i = 0; i < viewTrainingPlan.RowCount; i++)
                {
                    tblTrainingPlanDetailTableAdapter.InsertQuery(planId, viewTrainingPlan.GetRowCellValue(i, colnCrewid).ToString());
                }
                setTotal(thang, nam, courseID, viewTrainingPlan.RowCount);
            }
            else
            {
                tblTrainingPlanDetailTableAdapter.Update(dsTraining.tblTrainingPlanDetail);
                setTotal(thang, nam, courseID, dsTraining.tblTrainingPlanDetail.Rows.Count);
            }
        }


        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void txtStart_EditValueChanged(object sender, EventArgs e)
        {
            //DateEdit o = (DateEdit)sender;
            //if (o.DateTime.Month == DateTime.Now.Month && o.DateTime.Year == DateTime.Now.Year)
            //{

            //}
            //else
            //{
            //    XtraMessageBox.Show("Nhập ngày tháng không đúng", "Thông báo");
            //}
        }
    }
}